package cn.rubintry.animate.core

import android.view.View
import cn.rubintry.animate.core.ext.toFloat
import cn.rubintry.animate.core.param.AnimatorParam
import cn.rubintry.animate.core.param.AnimatorType
import cn.rubintry.animate.core.param.Value


/**
 * 作   者:  卢孙仲
 * 创建日期:  2023年6月25日
 * 描   述: 单view多动画控制类，用于开启关闭某个动画、配置参数等
 * 页面中文名:
 */
class MultiAnimatorCtl constructor(val view: View?){
    /**
     * 确保参数顺序正确
     */
    private val typeSet = linkedSetOf<AnimatorType>()


    /**
     * 启用平移动画
     */
    fun enableTranslation(): MultiAnimatorCtl {
        if(!typeSet.contains(AnimatorType.TRANSLATION)){
            typeSet.add(AnimatorType.TRANSLATION)
        }
        return this
    }

    /**
     * 启用缩放动画
     */
    fun enableScale(): MultiAnimatorCtl {
        if(!typeSet.contains(AnimatorType.SCALE)){
            typeSet.add(AnimatorType.SCALE)
        }
        return this
    }

    /**
     * 启用旋转动画
     */
    fun enableRotate(): MultiAnimatorCtl {
        if(!typeSet.contains(AnimatorType.ROTATE)){
            typeSet.add(AnimatorType.ROTATE)
        }
        return this
    }

    /**
     * 启用回弹动画
     */
    fun enableSprintScale(): MultiAnimatorCtl {
        if(!typeSet.contains(AnimatorType.SPRINT_SCALE)){
            typeSet.add(AnimatorType.SPRINT_SCALE)
        }
        return this
    }

    /**
     * 启用透明度动画
     */
    fun enableAlpha(): MultiAnimatorCtl {
        if(!typeSet.contains(AnimatorType.ALPHA)){
            typeSet.add(AnimatorType.ALPHA)
        }
        return this
    }

    /**
     * 填充参数值（如[TranslationAnimator]所需的坐标值等）
     */
    fun load(paramValue : Value) : MultiAnimator {
        //参数校验
        val readAnimatorType = mutableSetOf<AnimatorType>()
        try {
            //将参数赋值进动画里
            val animatorSet = mutableSetOf<IAnimatorInterface>()
            val iterator = typeSet.iterator()
            while (iterator.hasNext()){
                when(iterator.next()){
                    AnimatorType.TRANSLATION -> {
                        animatorSet.add(TranslationAnimator.obtain(view , paramValue.get(AnimatorType.TRANSLATION , AnimatorParam.OFFSET_X).toFloat() , paramValue.get(AnimatorType.TRANSLATION , AnimatorParam.OFFSET_Y).toFloat() ?: 0f , paramValue.get(AnimatorType.TRANSLATION , AnimatorParam.REVERSE) as? Boolean ?: false))
                    }

                    AnimatorType.SCALE -> {
                        animatorSet.add(ScaleAnimator.obtain(view , paramValue.get(AnimatorType.SCALE , AnimatorParam.FROM_SCALE).toFloat() ?: 1f , paramValue.get(AnimatorType.SCALE , AnimatorParam.TO_SCALE).toFloat() ?: 1f , paramValue.get(AnimatorType.SCALE , AnimatorParam.REVERSE) as? Boolean ?: false))
                    }

                    AnimatorType.ROTATE -> {
                        animatorSet.add(RotateAnimator.obtain(view , paramValue.get(AnimatorType.ROTATE , AnimatorParam.ANGLE).toFloat() ?: 0f , paramValue.get(AnimatorType.SCALE , AnimatorParam.REVERSE) as? Boolean ?: false))
                    }

                    AnimatorType.SPRINT_SCALE -> {
                        animatorSet.add(SpringScaleAnimator.obtain(view , paramValue.get(AnimatorType.SPRINT_SCALE , AnimatorParam.FROM_SCALE).toFloat() ?: 1f , paramValue.get(AnimatorType.SPRINT_SCALE , AnimatorParam.TO_SCALE).toFloat() ?: 1f , paramValue.get(AnimatorType.SPRINT_SCALE , AnimatorParam.FACTOR).toFloat() ?: 0f))
                    }

                    else -> {

                    }
                }
            }
            return MultiAnimator.obtain(view, *animatorSet.toTypedArray())
        }catch (e: Exception){
            e.printStackTrace()
        }
        return MultiAnimator(null)
    }
}